const Send_type = localStorage.getItem('SEND_TYPE')
const IP = localStorage.getItem('IP')
import request from "./request"



// 读发卡器
export const ReadCardIssuer = (): any => {

    if (Send_type == "HTTP") {
        return request({ url: IP + '/api/read/card/issuer', method: 'POST' })
    }

    if (Send_type == "DLL") {
        let data = {
            "cardclass": "readnfcparameter",
            "uuid": "readnfcparameter "
        }
        return DLLWrite(data)
    }

}


// 退卡
export const CleanCard = (): any => {
    let data = {
        cardclass: 'clearcard',
        lockclass: '1',
        shanqu: '1',
        pass: 'FFFFFFFFFFFF',
        uuid: '111974',
    };

    if (Send_type == "HTTP") {
        return request({ url: IP + '/api/card/clean', method: 'POST', data })
    }

    if (Send_type == "DLL") {
        return DLLWrite(data)
    }
}


// 读卡
export const Read_Card = (): any => {
    let data = { cardclass: 'readcard', uuid: 'setcard963', isclear: '0' };
    if (Send_type == "HTTP") {
        return request({ url: IP + '/api/card/read', method: 'POST', data })
    }

    if (Send_type == "DLL") {
        return DLLWrite(data)
    }


}



// 写卡
export const SendCard = (data: any): any => {
    
    if (Send_type == "HTTP") {
        return request({ url: IP + '/api/card/write', method: 'POST', data })
    }

    if (Send_type == "DLL") {
       return DLLWrite(data)
    }


}


// 写设置卡
export const SetupCard = (data: any): any => {

    if (Send_type == "HTTP") {
        return request({ url: IP + '/api/card/setup', method: 'POST', data })
    }


    if (Send_type == "DLL") {
       return DLLWrite(data)
    }

}



// 写挂失卡

export const Write_LossCard = (data: any): any => {

    if (Send_type == "HTTP") {
        return request({ url: IP + '/api/losscard/write', method: 'POST', data })
    }


    if (Send_type == "DLL") {
        return DLLWrite(data)
    }

}



interface DllWriteResponse<T> {
    data: T;
}

const DLLWrite = <T>(data: T): Promise<DllWriteResponse<T>> => {
    return new Promise((resolve) => {
        window.electronAPI.Dll_write(JSON.stringify(data)).then((res:DllWriteResponse<T>) => {
            resolve(JSON.parse(res) as DllWriteResponse<T>)
        });
    });
}
